package com.gopay.bis.web.controller.ebank;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.NjcbContants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.njcb.ebank.NjcbB2cMessage;

@Controller
@Scope("prototype")
public class NjcbB2cServerRes extends BankResBase {

	private static Log logger = LogFactory.getLog(NjcbB2cServerRes.class);

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		NjcbB2cMessage message = new NjcbB2cMessage();
		message.setMerchantID(request.getParameter("merchantID"));// 商户代码
		message.setMerOrderNum(request.getParameter("merOrderNum"));// 订单号
		message.setMerOrderAmt(request.getParameter("merOrderAmt"));// 订单金额
		message.setOrderId(request.getParameter("merOrderNum"));
		message.setBankOrderId(request.getParameter("merOrderNum"));
		message.setCurType(request.getParameter("curType"));// 支付币种
		message.setTranTime(request.getParameter("tranTime")); // 交易时间
		message.setOrderSerialNo(request.getParameter("orderSerialNo"));// 交易流水号
		message.setTranResult(request.getParameter("tranResult"));// 交易结果
		message.setComment(request.getParameter("comment"));// 失败原因
		message.setPlainMessage(request.getParameter("signDataStr"));// 明文
		message.setSignMessage(request.getParameter("signData"));// 密文
		message.setBank(BankConstants.BANK_LOGO_NJCB);
		message.setFront(false);
        //银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
		OrderInfo orderInfo = new OrderInfo();
		NjcbB2cMessage message = (NjcbB2cMessage) result.getModel("message");
		if (message != null) {
			if (NjcbContants.ORDER_TRANS_RESULT_SUCC.equals(message.getTranResult())) {
				orderInfo.setOrderId(message.getOrderId());
				orderInfo.setAmt(message.getMerOrderAmt());
				orderInfo.setNotifyNum("1");
				orderInfo.setPayStatus("00");
				orderInfo.setRetCode("00");
				orderInfo.setRetMsg("");
			} else {
				orderInfo.setOrderId(message.getOrderId());
				orderInfo.setAmt(message.getMerOrderAmt());
				orderInfo.setNotifyNum("1");
				orderInfo.setPayStatus("01");
				orderInfo.setRetCode("01");
				orderInfo.setRetMsg("");
			}
		}
		return orderInfo;
	}

	@RequestMapping(value = "njcbB2cServerRes")
	@Override
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- NjcbB2cServerRes bankback ---------------");
		return super.execute(request, response);
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("merchantID"))
			|| StringUtils.isEmpty(request.getParameter("merOrderNum"))
			|| StringUtils.isEmpty(request.getParameter("merOrderAmt"))
			|| StringUtils.isEmpty(request.getParameter("tranResult"))
			|| StringUtils.isEmpty(request.getParameter("signDataStr"))
			|| StringUtils.isEmpty(request.getParameter("signData"))) {
			return false;
		}
		return true;
	}
}
